﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Tame.Objects.Ship;
using Tame.Objects.Crew;
using Tame.Objects.Character;

namespace Tame.Objects
{

    /// <summary>
    /// A profile contains all the data for a character,ship,crew etc. For example,
    /// where before one might have selected data for a universe, you now have a new
    /// profile for each.
    /// </summary>
    static class ProfileInstance
    {
        /// <summary>
        /// Holds all profile instances at runtime.
        /// </summary>
        public static List<Instance> instances = new List<Instance>();

        public static bool addProfileInstance(Instance newInstance)
        {
            bool success = false;
            return success;
        }

        /// <summary>
        /// Should be called on application startup #FIXME
        /// </summary>
        /// <returns></returns>
        public static bool startupInitialiseProfiles()
        {
            bool success = false;

            readProfilesFromDisk();            

            return success;
        }

        /// <summary>
        /// Read the profile files from the tame directory,
        /// load them into the "instances" list. 
        /// </summary>
        /// <returns></returns>
        private static bool readProfilesFromDisk()
        {
            bool success = false;
            return success;
        }

        /// <summary>
        /// Check for modifications in instance, save to profile file 
        /// on disk if desired/necessary.
        /// </summary>
        /// <returns></returns>
        private static bool saveInstances()
        {
            // #FIXME -
            // for each instance
            // check each data storage class' modification flag.
            // if flag is set call object.save method inside class, passing in the profile name as parameter.
            // after that, if the list of methods > 0, then bring up a dialog asking if user wants
            // to save or not.
            bool success = false;
            return success;
        }

        public bool shutdownCheckForModifications()
        {
            // for each instance, call it's checkmodificationflag method.
            // if true, ask user for input, if they select yes then call saveInstances.
            bool success = false;
            return success;
        }
    }

    public class Instance
    {
        public string profileName; // {get;}
        public List<ShipClass> _ships;
        public List<PilotClass> _pilot;
        public List<CrewClass> _crew;

        /// <summary>
        /// This function should be called at program start. The common data storage classes
        /// can then be accessed by any class which requires info contained within them.
        /// </summary>
        public Instance(string name)
        {
            profileName = name;
            _ships = new List<ShipClass>();
            _crew = new List<CrewClass>();
            _pilot = new List<PilotClass>();
        }
       
        public bool checkModificationFlags()
        {
            /// #FIXME check each object for a modification flag. 
        }
    }

    static class LoadShipData
    {
        internal static List<ShipClass> GetShipData()
        {
            XmlDocument PardusData = new XmlDocument(); //* create an xml document object.
            PardusData.Load("pardusdata.xml"); //* load the XML document from the specified file.

            //* Find <Ships> and put that XML into a new XmlDocument (until I can figure out a better way to do this)
            XmlNodeList Ships = PardusData.GetElementsByTagName("Ships");
            XmlDocument ShipData = new XmlDocument();
            ShipData.LoadXml(Ships[0].OuterXml); //<Ships> becomes root element

            // make a few XmlNodeLists
            XmlNodeList xmlship = ShipData.GetElementsByTagName("ship");
            XmlNodeList xmlname = ShipData.GetElementsByTagName("name");
            XmlNodeList xmlmissile = ShipData.GetElementsByTagName("missile");
            XmlNodeList xmlgun = ShipData.GetElementsByTagName("gun");
            XmlNodeList xmlcapacity = ShipData.GetElementsByTagName("capacity");
            XmlNodeList xmlsize = ShipData.GetElementsByTagName("size");
            XmlNodeList xmlarmor = ShipData.GetElementsByTagName("armor");
            XmlNodeList xmlhull = ShipData.GetElementsByTagName("hull");
            XmlNodeList xmlcoins = ShipData.GetElementsByTagName("class");
            XmlNodeList xmlreq = ShipData.GetElementsByTagName("requirement");
            XmlNodeList xmlcost = ShipData.GetElementsByTagName("cost");
            XmlNodeList xmlcrew = ShipData.GetElementsByTagName("crew");
            XmlNodeList xmlfaction = ShipData.GetElementsByTagName("faction");
            XmlNodeList xmlavailable = ShipData.GetElementsByTagName("available");

            List<ShipClass> ShipList = new List<ShipClass>();
            for (int i = 0; i < xmlname.Count; i++)
            {

                string sid = xmlship[i].Attributes[0].Value;
                string sname = xmlname[i].InnerText;
                string smissile = xmlmissile[i].InnerText;
                string sgun = xmlgun[i].InnerText;
                string scapacity = xmlcapacity[i].InnerText;
                string ssize = xmlsize[i].InnerText;
                string sarmor = xmlarmor[i].InnerText;
                string shull = xmlhull[i].InnerText;
                string scoins = xmlcoins[i].InnerText;
                string srequirement = xmlreq[i].InnerText;
                string sreqtype = xmlreq[i].Attributes[0].Value;
                string scost = xmlcost[i].InnerText;
                string screw = xmlcrew[i].InnerText;
                string sfaction = xmlfaction[i].InnerText;
                string savailable = xmlavailable[i].InnerText;
                ShipClass foo = new ShipClass(sid, sname, smissile, sgun, scapacity, ssize, sarmor, shull, scoins, srequirement, sreqtype, scost, screw, sfaction, savailable);
                ShipList.Add(foo);
            }
            return (ShipList);
        }


    }
}
